package com.yw.smartcityforjava.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yw.smartcityforjava.App;
import com.yw.smartcityforjava.R;
import com.yw.smartcityforjava.api.ICallback;
import com.yw.smartcityforjava.api.ServiceCreator;
import com.yw.smartcityforjava.api.register.RegisterRequest;
import com.yw.smartcityforjava.api.register.RegisterRes;

public class RegisterActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView titleTv;
    private EditText accountEt;
    private EditText pwdEt;
    private EditText repwdEt;
    private EditText phoneEt;
    private Button btnRegister;
    private TextView loginTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        titleTv = (TextView) findViewById(R.id.titleTv);
        accountEt = (EditText) findViewById(R.id.accountEt);
        pwdEt = (EditText) findViewById(R.id.pwdEt);
        repwdEt = (EditText) findViewById(R.id.repwdEt);
        phoneEt = (EditText) findViewById(R.id.phoneEt);
        btnRegister = (Button) findViewById(R.id.btn_register);
        loginTv = (TextView) findViewById(R.id.loginTv);

        TextView titleTv = toolbar.findViewById(R.id.titleTv);
        titleTv.setText("注册");


        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String username = accountEt.getText().toString().trim();
                String pwd = pwdEt.getText().toString().trim();
                String repwd = repwdEt.getText().toString().trim();
                String phone = phoneEt.getText().toString().trim();

                if(TextUtils.isEmpty(username)){
                    Toast.makeText(App.app, "请输入用户名！", Toast.LENGTH_LONG).show();
                    return;
                }
                if(TextUtils.isEmpty(pwd)){
                    Toast.makeText(App.app, "请输入密码！", Toast.LENGTH_LONG).show();
                    return;
                }
                if(!pwd.equals(repwd)){
                    Toast.makeText(App.app, "两次密码不一致！", Toast.LENGTH_LONG).show();
                    return;
                }

                if(TextUtils.isEmpty(phone)){
                    Toast.makeText(App.app, "请输入手机号！", Toast.LENGTH_LONG).show();
                    return;
                }
                registerRequest(username, pwd, phone);
            }
        });
        loginTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();
            }
        });
    }

    private void registerRequest(String username, String pwd, String phone) {

        ServiceCreator.getInstance().getApi().register(new RegisterRequest(username,pwd,phone)).enqueue(new ICallback<RegisterRes>() {
            @Override
            public void onSuccess(RegisterRes response) {
                if(response.getCode()==200){
                    Toast.makeText(App.app, "注册成功!", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                    finish();
                }else{
                    Toast.makeText(App.app, response.getMsg(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}